const goods = require('../model/goodsModel.js')


async function add(ctx) {
    const goodsCode = new Date().getTime()
    let data = {
        number: goodsCode,
        name: ctx.request.body.name,
        price: ctx.request.body.price,
        describes: ctx.request.body.describes,
        cid: ctx.request.body.cid,
        stock: ctx.request.body.stock,
        photo: ctx.request.body.photo,
    }
    console.log(data);
    let result = await goods.add(data)
    ctx.body = result
}

async function list(ctx) {
    let page = ctx.request.query.page || 1;
    let num = ctx.request.query.num || 10;

    let cid = ctx.request.query.cid;
    let name = ctx.request.query.name;
    let result = await goods.list(page, num, { cid,name });

    if (result.total) {
        ctx.body = { code: 1, msg: '', data: result }
    } else {
        ctx.body = { code: 0, msg: '暂无数据', data: '' }
    }
}

async function mod(ctx) {

    let data = {
        id: ctx.params.id,
        name: ctx.request.body.name,
        price: ctx.request.body.price,
        describes: ctx.request.body.describes,
        cid: ctx.request.body.cid,
        stock: ctx.request.body.stock,
        photo: ctx.request.body.photo,
    }
    
    let res = await goods.mod(data);
    if (res.code) {
        ctx.body = res
    } else {
        ctx.body = res
    }
}

async function get(ctx) {

    let data={id:ctx.params.id};
    let res=await goods.get(data);
    if(res){
        return ctx.body={code:1,msg:'',data:res}
    }else{
        return ctx.body={code:0,msg:'未查到该数据',data:''}
    }
}

// async function del(ctx){
//     let data={id:ctx.request.query.id};
//     let res=await goods.del(data);
//     if(res){
//         return ctx.body={code:1,msg:'删除成功',data:''}
//     }else{
//         return ctx.body={code:0,msg:'删除失败，请检查以下重新操作',data:''}
//     }
// }
module.exports = { add, list, mod, get }